﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DATN.mds.Product
{
    public partial class ProductIndext : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Us_Id"] != null)
            {
                if (!IsPostBack)
                {
                    //Session["Cus_Id"] = "";
                    //Bind data vào GridView
                    load_data();

                }
            }
            else
            {
                Response.Redirect("../Account/Login.aspx");
            }
            btnCheckBox.Attributes.Add("onclick", "return confirm('Bạn có chắc chắn xóa không?');");
        }

        protected void load_data()
        {
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
            cnn.Open();

            SqlDataAdapter da = new SqlDataAdapter("Select * From Products Where Us_Id = " + Convert.ToInt32(Session["Us_Id"].ToString()), cnn);
            DataSet ds = new DataSet();
            da.Fill(ds, "Products");

            GridView1.DataSource = ds.Tables["Products"];
            GridView1.DataBind();

            ds.Dispose();
            da.Dispose();
            cnn.Close();

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                // get the categoryID of the clicked row
                try
                {
                    int Pro_Id = Convert.ToInt32(e.CommandArgument);
                    // Create New Session And Redirect to delete page
                    Session["Pro_Id"] = Pro_Id.ToString();
                    //Response.Redirect("/mds.Note/DeleteNote.aspx");
                }
                catch (Exception ex)
                {
                    Response.Write(ex);
                }
            }
            if (e.CommandName == "Edit")
            {
                // get the categoryID of the clicked row
                try
                {
                    int Pro_Id = Convert.ToInt32(e.CommandArgument);
                    // Create New Session And Redirect to delete page
                    Session["Pro_Id"] = Pro_Id.ToString();
                    Response.Redirect("/mds.Product/EditProduct.aspx");
                }
                catch (Exception ex)
                {
                    Response.Write(ex);
                }
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
                l.Attributes.Add("onclick", "javascript:return " +
                "confirm('Bạn muốn sửa nội dung trong cột này! - " + "')");

                LinkButton l2 = (LinkButton)e.Row.FindControl("LinkButton2");
                l2.Attributes.Add("onclick", "javascript:return " +
                "confirm('Bạn muốn xóa nội dung trong cột này! - " + "')");
            }
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
            {
                CheckBox chkBxSelect = (CheckBox)e.Row.Cells[0].FindControl("chkBxSelect");
                CheckBox chkBxHeader = (CheckBox)this.GridView1.HeaderRow.FindControl("chkBxHeader");
                HiddenField hdnFldId = (HiddenField)e.Row.Cells[0].FindControl("hdnFldId");

                chkBxSelect.Attributes["onclick"] = string.Format("javascript:ChildClick(this,document.getElementById('{0}'),'{1}');", chkBxHeader.ClientID, hdnFldId.Value.Trim());
            }

            if (e.Row.DataItemIndex == -1)
                return;

            e.Row.Attributes.Add("onMouseOver", "this.style.cursor='hand';");
            if ((e.Row.RowIndex % 2) == 0)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#aafdaa'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#b4cbd5'");
            }
            else
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#aafdaa'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
            string ac = "Delete From Products Where Pro_Id =" + Convert.ToInt32(Session["Pro_Id"].ToString());

            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = ac;

            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();
            load_data();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect("~/mds.Product/CreateProduct.aspx");
        }

        protected void btnCheckBox_Click(object sender, EventArgs e)
        {
            //Get Ids
            string[] IDs = hdnFldSelectedValues.Value.Trim().Split('|');
            //lb_IdCheckBox.Text = "User Id Da checked là:";
            if (IDs.Length > 0)
            {
                for (int i = 0; i < IDs.Length; i++)
                {
                    //Label8.Text += CheckMail(IDs[i]) + ";";
                    //AllMail += CheckMail(IDs[i]) + ",";
                    //lb_IdCheckBox.Text += IDs[i] + ";";
                    int id = int.Parse(IDs[i]);
                    DeleteMany(id);
                }
                //Session["SendAllMail"] = AllMail;
                //Response.Redirect("../mds.MailManagement/SendMailList.aspx");
            }
            else
            {
                //Label8.Text = "Bạn Chưa chọn Khách Hàng Nào cả!";
                lb_IdCheckBox.Text = "Bạn Chưa chọn Khách Hàng Nào cả!";
            }
            load_data();
        }
        
        protected void DeleteMany(int id)
        {
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
            //int xid = id;
            string com = "Delete From Products Where Pro_Id = " + id;

            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = com;

            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();
            //load_data();
            //Page.Response.Redirect("~/mds.Note/IndexNote.aspx");

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            load_data();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("../ImportExcel/ImportProduces.aspx");
        }
   
    }
}